<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models\Adminer;

use App\Models\Adminer\Menus;
use Illuminate\Database\Eloquent\Model as Eloquent;

/**
 * Class RoleMenu
 * 
 * @property int $role_id
 * @property int $menu_id
 *
 * @package App\Models
 */
class RoleMenu extends Eloquent
{
	protected $table = 'role_menu';
	public $incrementing = false;
	public $timestamps = false;

	protected $casts = [
		'role_id' => 'int',
		'menu_id' => 'int'
	];

	protected $fillable = [
		'role_id',
		'menu_id'
	];

    public static function isPermission($role_id, string $path): bool
    {
        return self::where('role_id', $role_id)->where('menu_id', Menus::where('api', $path)->value('id'))->exists();
    }

    public static function getByRoleId(string $role_id): ?array
    {
        $menu_ids = self::where('role_id', $role_id)->select('menu_id')->get();

        if(!$menu_ids) {
            return [];
        }

        return array_column($menu_ids->toArray(), 'menu_id');
    }
}
